package com.echo.fm.user.role;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.criterion.Criterion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.echo.fm.user.role.model.Role;
import com.echo.util.ErrorException;
import com.echo.util.JSONResult;
import com.echo.util.ResultUtils;
import com.echo.util.TimeGetter;
import com.echo.util.impl.ValidateUtils;

@Controller
public class IRoleController {
	@Autowired
	@Qualifier("roleService")
	IRoleService roleService;

	@RequestMapping(value = "/web/role/addSave", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	Object addRole(HttpServletRequest request, HttpServletResponse response) throws ErrorException {
		try {
			// Admin detail = HeaderParser.getUser(request);
			// if (detail == null) {
			// throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
			// }
			Role role = new Role();
			String ename = request.getParameter("ename");
			if (ValidateUtils.isNotEmpty(ename)) {
				role.setEname(ename);
			}
			String cname = request.getParameter("cname");
			if (ValidateUtils.isNotEmpty(cname)) {
				role.setCname(cname);
			}
			role.setT(TimeGetter.getTime());
			String status = request.getParameter("status");
			if (ValidateUtils.isNotEmpty(status)) {
				role.setStatus(Integer.valueOf(status));
			}
			String intro = request.getParameter("intro");
			if (ValidateUtils.isNotEmpty(intro)) {
				role.setIntro(intro);
			}
			String id = request.getParameter("id");
			if (ValidateUtils.isNotEmpty(id)) {
				role.setId(Integer.valueOf(id));
			}
			roleService.createRole(role);
			return ResultUtils.instJSONResult(ResultUtils.WEB_SUCCESS_CODE);
		} catch (ErrorException e) {
			e.printStackTrace();
			return ResultUtils.instJSONResult(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		}

	}

	@RequestMapping(value = "/web/role/delete", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	Object deleteRole(HttpServletRequest request, HttpServletResponse response) {
		try {
			// Admin detail = HeaderParser.getUser(request);
			// if (detail == null) {
			// throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
			// }
			String id = request.getParameter("id");
			Role role = roleService.searchRoleById(id);
			if (role != null) {
				roleService.deleteRole(role);
			}
			return ResultUtils.instJSONResult(ResultUtils.WEB_SUCCESS_CODE);
		} catch (ErrorException e) {
			e.printStackTrace();
			return ResultUtils.instJSONResult(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		}

	}

	@RequestMapping(value = "/web/role/updateSave")
	@ResponseBody
	Object updateRole(HttpServletRequest request, HttpServletResponse response) {
		try {
			// Admin detail = HeaderParser.getUser(request);
			// if (detail == null) {
			// throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
			// }
			String id = request.getParameter("id");
			Role roleExist = roleService.searchRoleById(id);
			if (roleExist != null) {
				String ename = request.getParameter("ename");
				if (ValidateUtils.isNotEmpty(ename)) {
					roleExist.setEname(ename);
				}
				String cname = request.getParameter("cname");
				if (ValidateUtils.isNotEmpty(cname)) {
					roleExist.setCname(cname);
				}
				String status = request.getParameter("status");
				if (ValidateUtils.isNotEmpty(status)) {
					if ("on".equals(status)) {
						roleExist.setStatus(1);
					} else {
						roleExist.setStatus(0);
					}
				}
				roleExist.setT(TimeGetter.getTime());
				String intro = request.getParameter("intro");
				if (ValidateUtils.isNotEmpty(intro)) {
					roleExist.setIntro(intro);
				}
			}
			roleService.updateRole(roleExist);
			return ResultUtils.instJSONResult(ResultUtils.WEB_SUCCESS_CODE);

		} catch (ErrorException e) {
			e.printStackTrace();
			return ResultUtils.instJSONResult(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		}
	}

	@RequestMapping(value = "/web/role/list")
	@ResponseBody
	Object roleList(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {

		try {
			// Admin detail = HeaderParser.getUser(request);
			// if (detail == null) {
			// throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
			// }

			Integer status = null;
			String statusStr = request.getParameter("status");
			if (ValidateUtils.isNotEmpty(statusStr)) {
				status = Integer.valueOf(statusStr);
			}

			String cname = null;
			String cnameStr = request.getParameter(cname);
			if (ValidateUtils.isNotEmpty(cnameStr)) {
				cname = new String(cnameStr.getBytes("ISO-8859-1"), "UTF-8");
			}

			String ename = null;
			String enameStr = request.getParameter("ename");

			if (ValidateUtils.isNotEmpty(enameStr)) {
				ename = new String(enameStr.getBytes("ISO-8859-1"), "UTF-8");
			}
			List<Criterion> criterions = new ArrayList<Criterion>();
			List<Role> roles = roleService.searchRoleList(criterions);
			Map<String, List<Role>> map = new HashMap<String, List<Role>>();
			map.put("list", roles);
			JSONResult result = ResultUtils.instJSONResult(ResultUtils.WEB_SUCCESS_CODE);
			result.setData(map);
			return result;
		} catch (ErrorException e) {
			e.printStackTrace();
			return ResultUtils.instJSONResult(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		}
	}

}
